        TITL DAI FIRMWARE 2E5FC-2E71A  V1.1
        ORG   :E5FC
*
*
*
*******************************
* INITIALISE HEADER / TRAILER *
*******************************
*
* Sets up 4 background colour lines which can act
* as header/trailer. Sets up colours in colour RAM.
* The header/trailer area consists of 4 groups of
* 4 bytes: 00 00 xx 3x, in which xx is the colour
* and 3x the mode word:
*        x=6: Header.
*        x=F: Trailer.
*        x=0: Middle area (split mode).
*
* Entry: HL: 1st byte header/trailer area.
*        DE: Address table with required colours.
*        A : Screen mode.
*        B : Depth -1 in scans of each blanking
*            line: 06 (header), 0F (trailer),
*            00 (middle).
* Exit:  HL: Points after header/trailer area.
*        AFBCDE preserved.
*
SSUBL   PUSH  PSW
        PUSH  B
        PUSH  D
        MOV   C,A       Store screen mode in C
        MOV   A,B
        ORI   :30       Set 4 colour to make mode
 REM word + rept.count
        PUSH  PSW       Save mode word on stack
        MVI   B,:00
        MOV   A,E       Get lobyte addr colours
        SUI   :7C
        JZ    SBL10     If char mode then 4 colours
        MOV   A,C       Get screen mode
        RAR
        RAR
        MOV   C,B
        JC    SBL20     Jump if 4-colour mode
 REM
* 16-colour modes only:
 REM
        POP   PSW       Restore header/trailer info
        ORI   :80       Set 16-colour (msb=1)
        PUSH  PSW
        LDAX  D         Get colour
        ADD   A         )
        ADD   A         ) Move lonibble into
        ADD   A         ) hinibble
        ADD   A         )
        MVI   B,:FF     Set all foreground
SBL10   MOV   C,A       Colour in C
 REM
* Load header/trailer:
 REM
SBL20   POP   PSW       Get mode word
        PUSH  PSW
        MOV   M,A       Load 1st byte pointer
        DCX   H         Next addr in block
        LDAX  D         Get colour info
        INX   D
        MOV   M,A       Load 2nd byte
        DCX   H         Next addr in block
        MOV   M,B       Load 3rd byte
        DCX   H
        MOV   M,C       Load 4th byte
        DCX   H
        CPI   :B0       All blocks done ?
        JC    SBL20     Next one if not
        POP   PSW
        POP   D
        POP   B
        POP   PSW
        RET
*
*****************************
* SET UP A 4-LINE TEXT AREA *
*****************************
*
* Locates the last few lines of text on the
* screen. If the screen was in split mode, the
* whole contents of the old screen is located.
* If it was mode 0, the last few lines above
* and the cursor line are located.
* The text is then moved to a required position,
* including the cursor, etc.
*
* Entry: HL: Points to address where the text to
*            be put.
* Exit:  HL: Points to new top of text.
*        AFBCDE preserved.
*
SMVTXT  PUSH  PSW
        PUSH  B
        PUSH  D
        MOV   B,H       ) New top of text in BC
        MOV   C,L       )
        LHLD  CHSO      Get previous start char
        PUSH  H         on stack
        XCHG            and in DE
        LXI   H,:FDE8   Length split screen char
 REM area
        DAD   D         Calc 1st line mode byte
 REM outside screen frame
        XCHG            in DE
        LHLD  CURSOR    Get cursor pos addr
        CALL  COMP      Check if cursor is still
 REM inside frame
        JC    SMV20     Jump if not
        XCHG            HL is addr 1st line mode
 REM outside screen frame
        POP   D         DE is prev start char
SMV10   PUSH  H         Save end preserved text area
        LHLD  CURSOR    Get cursor pos addr
        CALL  CURDEL    Delete cursor
        XTHL            Previous start char in HL
        CALL  MOVES     Roll screen area to new top
 REM of text; cursor on last line
        XTHL            Cursor pos in HL
        CALL  SUBDE     Calc cursor pos against new
 REM frame start
        DAD   B         Calc new cursor pos addr
        CALL  CURSET    Keep cursor on same pos on
 REM line
        LHLD  LNSTR     Get old start line pointer
        CALL  SUBDE     HL=HL-DE
        DAD   B         Calc new cursor pos
        CALL  SSETL     Store addr line mode byte
 REM current line and last addr
 REM on that line
        POP   H         Get end preserved text area
        CALL  SUBDE     HL=HL-DE
        DAD   B
        POP   D
        POP   B
        POP   PSW
        RET
 REM
* Scroll frame 1 line if cursor outside frame:
 REM
SMV20   POP   H
        LHLD  LNSTR     Get startaddr cursor line
        LXI   D,:FF7A
        DAD   D         HL: start line after cursor
        PUSH  H
        LXI   D,:0218
        DAD   D         Subtract 4 lines and get
        XCHG            line mode byte in DE
        POP   H         Get end reqd area
        JMP   SMV10   
*
*********************************
* PLACE CURSOR AT BEGIN OF LINE *
*********************************
*
* Sets the cursor at the beginning of a line.
* Several pointers are updated.
*
* SSETC: Given a pointer to the start of line,
*        sets start and end line variables and
*        places the cursor at the beginning of
*        the line.
* SSETL: Sets only start and end line positions.
*
* Entry: HL: Address line mode byte current line.
* Exit:  ABCDEHL preserved.
*
SSETC   PUSH  PSW
        PUSH  D
        PUSH  H
        LXI   D,TICC8
        DAD   D         Get addr 1st data byte
 REM on current line
        CALL  CURSET    Put cursor on screen
        XRA   A
        STA   ERRCOD    No extended lines
        POP   H
        POP   D
        POP   PSW
SSETL   PUSH  PSW
        SHLD  LNSTR     Store addr line mode byte
 REM current line
        MVI   A,:80     ) Calc lobyte last addr
        ADD   L         ) on this line
        STA   LINLEN    Store it in LNEND
        POP   PSW
        RET
*
************************
* SET GRAPHICS COLOURS *
************************
*
* Sets the colours available in a 4 colour mode
* and the initial background in a 16 colour mode.
*
* Entry: HL: Points to colours vector.
* Exit:  All registers preserved.
*
SCOLG   PUSH  PSW
        PUSH  B
        PUSH  D
        PUSH  H
        LXI   D,COL2    Addr 1st COLORG byte
        CALL  VCOPY     Set COLORG parameters
        LDA   COL1      Get current screen mode
        ORA   A         Check mode type
        LHLD  SCTOP     Get addr after header
        CP    BCOLS     If not mode 0: Load COLORG
 REM parameters in header
        RAR             Check if char mode
        LHLD  SCE       Get addr after trailer
        CNC   BCOLS     If all graphics mode:
 REM Load colours in trailer
SGC10   JMP   XRET      Popall, ret
*
********************
* MOVE SCREEN AREA *
********************
*
* Moves a block of screen data from any position
* to any other.
*
* Entry: BC: Points to highaddress target area.
*        DE: Points to highaddress source area.
*        HL: Points to lowaddress -1 source area.
* Exit:  All registers preserved.
*
MOVES   PUSH  PSW
        PUSH  B
        PUSH  D
        PUSH  H
        CALL  SUBDE     Calc length of block
 REM (neg.value)
        MOV   A,E
        SUB   C
        MOV   A,D
        SBB   B
        JC    MVS20     Jump if move up
 REM
* Move down:
 REM
        MOV   D,H       ) Length in DE
        MOV   E,L       )
        DAD   B         HL = lowest targetaddr -1
        POP   B         BC = lowest sourceaddr -1
        PUSH  B
MVS10   MOV   A,D
        ORA   E
        JZ    MVS40     Abort if ready
        INX   D
        INX   H
        INX   B
        LDAX  B         Get byte from source area
        MOV   M,A       and move it into target area
        JMP   MVS10     Next one
 REM
* Move up:
 REM
MVS20   MOV   A,H
        ORA   L
        JZ    MVS40     Quit if ready
        INX   H
        LDAX  D         Get byte from source area
        STAX  B         Move it into target area
        DCX   B
        DCX   D
        JMP   MVS20     Next one
*
MVS40   JMP   XRET      Popall, ret
*
****************
* HL = HL - DE *
****************
*
* Entry: None.
* Exit:  HL=HL-DE.
*        Other registers preserved.
*
SUBDE   PUSH  PSW
        MOV   A,L
        SUB   E
        MOV   L,A       L=L-E
        MOV   A,H
        SBB   D
        MOV   H,A       H=H-D
        POP   PSW
        RET
*
*******************
* COMPARE HL - DE *
*******************
*
* Compares HL with DE (HL-DE).
*
* Exit: Z=0: Not identical:
*            CY=0: DE < HL.
*            CY=1: DE > HL.
*       Z=1: Identical.
*       AF corrupted, BCDEHL preserved.
*
COMP    MOV   A,H
        SUB   D
        RNZ
        MOV   A,L
        SUB   E
        RET
*
*************************
* ADD OFFSET TO ADDRESS *
*************************
*
* Sets HL = HL + A.
*
* Entry: HL: baseaddress.
*        A : offset.
* Exit:  HL = HL + A.
*        BCDE preserved.
*
DADA    ADD   L         Add lobyte addr to offset
        MOV   L,A       and store it in L
        RNC
        INR   H         Incr hibyte if overflow
        RET
*
**********************************
* TWO COMPLEMENT OF 16-BITS DATA *
**********************************
*
* Sets HL = - HL.
*
* Entry: Data to be complemented in HL.
* Exit:  HL contains two-complement.
*        AFBCDE preserved.
*
CMPHL   PUSH  PSW
        MOV   A,L
        CMA             Compl. L
        MOV   L,A       and store it
        MOV   A,H
        CMA             Compl. H
        MOV   H,A       and store it
        INX   H         Add 1
        POP   PSW
        RET
*
****************************
* DRAW A DOT ON THE SCREEN *
****************************
*
* Draws a single blob of a colour anywhere
* on the screen.
*
* Entry: C,HL: Y,X coordinate of the dot.
*        A:    Colour of the dot.
* Exit:  CY=0: O.K.
*        CY=1: Error code in A.
*        ABCDEHL preserved.
*
SDOT    ORA   A
        PUSH  PSW
        PUSH  B
        PUSH  D
        PUSH  H
        MOV   B,C       Y-coord in B
        MOV   D,H       ) X-coord in DE
        MOV   E,L       )
        JMP   FIL10     Into 'SFILL'
*
*
*
        UNL
CHSO    EQU   :009B
CURSOR  EQU   :0072
CURDEL  EQU   :E36B
CURSET  EQU   :E330
LNSTR   EQU   :0078
TICC8   EQU   :FFF8
ERRCOD  EQU   :007B
LINLEN  EQU   :007A
COL2    EQU   :009E
VCOPY   EQU   :E254
COL1    EQU   :009D
SCTOP   EQU   :0082
BCOLS   EQU   :E267
SCE     EQU   :008E
XRET    EQU   :E138
FIL10   EQU   :E81D
        LST
        END
